package cn.psvmc.cxzapi.controller.customer;

import cn.psvmc.cxzapi.model.common.*;
import cn.psvmc.cxzapi.model.customer.TCustomerAuth;
import cn.psvmc.cxzapi.model.goods.TGoodsExample;
import cn.psvmc.cxzapi.model.order.TOrder;
import cn.psvmc.cxzapi.model.order.TOrderItem;
import cn.psvmc.cxzapi.model.ticket.TTicket;
import cn.psvmc.cxzapi.model.ticket.TTicketReplies;
import cn.psvmc.cxzapi.response.*;
import cn.psvmc.cxzapi.service.customer.TCustomerGoodsService;
import cn.psvmc.cxzapi.util.PageUtil;
import cn.psvmc.cxzapi.util.ToolUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author ywx
 * @className TCustomerGoodsController
 * @description
 * @date 2024/9/1 17:57
 **/
@RestController
@RequestMapping("/mpapi/customer_goods")
public class TCustomerGoodsController {
    @Resource
    private TCustomerGoodsService customerGoodsService;

    /**
     * @Description 商品分类列表
     * @Date 2024/9/1 0:01
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_goods_sort")
    public Result listGoodsSort() {
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.listGoodsSort());
    }

    /**
     * @Description 商品列表
     * @Date 2024/8/29 22:08
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.PageResult2
     **/
    @PostMapping("/list_goods")
    public PageResult2 listGoods(@RequestBody GoodsEntity entity) {
        Page<TGoodsExample> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(customerGoodsService.listGoods(queryPage, entity));
    }

    /**
     * @Description 根据商品id获取商品列表
     * @Date 2024/9/28 14:22
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_goods_by_ids")
    public Result listGoodsByIds(@RequestBody GoodsEntity entity) {
        ToolUtils.validation(new Object[]{entity.getGoodsIds(), "商品ids", 2});
        entity.setSort(1);
        return ResultGenerator.genSuccessResult(customerGoodsService.listGoodsByIds(entity));
    }

    /**
     * @Description 客户浏览记录推荐商品列表
     * @Date 2024/9/1 17:44
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.PageResult2
     **/
    @PostMapping("/list_recommended_goods")
    public PageResult2 listRecommendedGoods(@RequestBody OrderEntity entity) {
        Page<TOrder> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(customerGoodsService.listRecommendedGoods(queryPage, entity));
    }

    /**
     * @Description 商品详情
     * @Date 2024/9/1 11:33
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_goods")
    public Result detailGoods(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "商品id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.detailGoods(id));
    }

    /**
     * @Description 商品规格列表
     * @Date 2024/9/1 14:09
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/goods_specs")
    public Result goodsSpecs(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "商品id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.goodsSpecs(id));
    }

    /**
     * @Description 自动领券
     * @Date 2024/9/1 15:10
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/auto_coupon")
    public Result autoCoupon(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "商品id", 1});
        customerGoodsService.autoCoupon(id);
        return ResultGenerator.genSuccessResult("领取成功！");
    }

    /**
     * @Description 商品规格优惠券列表
     * @Date 2024/9/4 8:41
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_specs_coupon")
    public Result listSpecsCoupon(@RequestBody GoodsSpecsEntity entity) {
        ToolUtils.validation(new Object[]{
                entity.getGsid(), "商品规格id", 1
                , entity.getDay(), "天数", 1
                , entity.getNum(), "数量", 1
        });
        return ResultGenerator.genSuccessResult("获取成功！", customerGoodsService.listSpecsCoupon(entity));
    }

    /**
     * @Description 获取商品规格价格
     * @Date 2024/9/25 8:59
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/goods_price2")
    public Result goodsPrice2(@RequestBody GoodsSpecsEntity entity) {
        ToolUtils.validation(new Object[]{
                entity.getGsid(), "商品规格id", 1
                , entity.getDay(), "天数", 1
                , entity.getNum(), "数量", 1
        });
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.goodsPrice2(entity));
    }

    /**
     * @Description 确认订单
     * @Date 2024/9/1 15:58
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/confirm_order")
    public Result confirmOrder(@RequestBody GoodsSpecsEntity entity) {
        ToolUtils.validation(new Object[]{
                entity.getGsid(), "商品规格id", 1
                , entity.getDay(), "天数", 1
                , entity.getNum(), "数量", 1
        });
        return ResultGenerator.genSuccessResult("确认成功！", customerGoodsService.confirmOrder(entity));
    }

    /**
     * @Description 下单
     * @Date 2024/9/3 8:30
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_order")
    public Result addOrder(@RequestBody GoodsSpecsEntity entity) {
        ToolUtils.validation(new Object[]{
                entity.getGsid(), "商品规格id", 1
                , entity.getDay(), "天数", 1
                , entity.getNum(), "数量", 1
                , entity.getUsername(), "姓名", 2
                , entity.getUserAddress(), "地址", 2
                , entity.getUserphone(), "手机号", 2
        });
        entity.setAddType(1);
        return ResultGenerator.genSuccessResult("下单成功！", customerGoodsService.addOrder(entity));
    }

    /**
     * @Description 快捷下单查询租金、分期计划、免押风控的信息
     * @Date 2025/1/14 23:50
     * @Author YWX
     * @Param [item]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/rent_order_init")
    public Result rentOrderInit(@RequestBody TOrderItem item) {
        ToolUtils.validation(new Object[]{
                item.getOutSkuId(), "商品规格id", 2
                , item.getDuration(), "天数", 1
                , item.getQuantity(), "数量", 1
                , item.getRentStartTime(), "租赁开始时间", 2
        });
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.rentOrderInit(item));
    }

    /**
     * @Description 快捷下单-创建业务订单
     * @Date 2025/1/15 21:09
     * @Author YWX
     * @Param [item]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/rent_order_submit")
    public Result rentOrderSubmit(@RequestBody Map<String, Object> item) {
        return ResultGenerator.genSuccessResult("创建成功！", customerGoodsService.rentOrderSubmit(item));
    }

    /**
     * @Description 用户可领取优惠券列表
     * @Date 2024/9/1 23:01
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_goods_coupon")
    public Result listGoodsCoupon(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "商品id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.listGoodsCoupon(id));
    }

    /**
     * @Description 获取用户是否认证
     * @Date 2024/9/1 22:19
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/get_customer_auth")
    public Result getCustomerAuth() {
        return ResultGenerator.genSuccessResult("确认成功！", customerGoodsService.getCustomerAuth());
    }

    /**
     * @Description 上传认证附件
     * @Date 2024/9/2 0:29
     * @Author YWX
     * @Param [auth]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/upload_auth")
    public Result uploadAuth(MultipartFile file) {
        ToolUtils.validation(new Object[]{
                file, "文件", 2
        });
        return customerGoodsService.uploadAuth(file);
    }

    /**
     * @Description 认证
     * @Date 2024/9/4 22:27
     * @Author YWX
     * @Param [auth]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_auth")
    public Result addAuth(@RequestBody TCustomerAuth auth) {
        ToolUtils.validation(new Object[]{
                auth.getFront(), "身份证正面", 2
                , auth.getName(), "姓名", 2
                , auth.getIdNumber(), "身份证号码", 2
                , auth.getValidPeriod(), "有效期限", 2
                , auth.getBack(), "身份证背面", 2
                , auth.getEmergencyContactName(), "紧急联系人", 2
                , auth.getEmergencyContactPhone(), "紧急联系人电话", 2
                , auth.getEmergencyContactName2(), "紧急联系人2", 2
                , auth.getEmergencyContactPhone2(), "紧急联系人电话2", 2
        });
        return customerGoodsService.addAuth(auth);
    }

    /**
     * @Description 工单类型选择列表
     * @Date 2024/9/2 8:19
     * @Author YWX
     * @Param []
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_ticket_type")
    public Result listTicketType() {
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.listTicketType());
    }

    /**
     * @Description 工单原因选择列表
     * @Date 2024/9/2 8:21
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_ticket_cause")
    public Result listTicketCause(@RequestBody MarketEntity entity) {
        Integer typeId = entity.getTypeId();
        ToolUtils.validation(new Object[]{typeId, "工单类型id", 1});
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.listTicketCause(typeId));
    }

    /**
     * @Description 添加工单
     * @Date 2024/9/2 8:24
     * @Author YWX
     * @Param [tTicket]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_ticket")
    public Result addTicket(@RequestBody TTicket tTicket) {
        ToolUtils.validation(new Object[]{
                tTicket.getTypeId(), "工单类型id", 1
                , tTicket.getTcid(), "工单原因id", 1
                , tTicket.getOrderCode(), "订单编号", 2
                , tTicket.getTitle(), "工单标题", 2
        });
        int addResult = customerGoodsService.addTicket(tTicket);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
     * @Description 工单回复
     * @Date 2024/9/2 8:29
     * @Author YWX
     * @Param [tTicketReplies]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/add_ticket_replies")
    public Result addTicketReplies(@RequestBody TTicketReplies tTicketReplies) {
        ToolUtils.validation(new Object[]{
                tTicketReplies.getTicketId(), "工单id", 1
                , tTicketReplies.getReplier(), "回复人", 2
                , tTicketReplies.getComm(), "回复内容", 2
        });

        int addResult = customerGoodsService.addTicketReplies(tTicketReplies);
        if (addResult > 0) {
            return ResultGenerator.genSuccessResult("保存成功！");
        }
        return ResultGenerator.genFailResult("保存失败！");
    }

    /**
     * @Description 工单回复详情
     * @Date 2024/9/2 8:32
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/detail_ticket_replies")
    public Result detailTicketReplies(@RequestBody DetailEntity entity) {
        Integer id = entity.getId();
        ToolUtils.validation(new Object[]{id, "工单id", 1});
        return ResultGenerator.genSuccessResult("查询成功", customerGoodsService.detailTicketReplies(id));
    }

    /**
     * @Description 我的工单
     * @Date 2024/9/7 10:49
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.PageResult2
     **/
    @PostMapping("/list_my_ticket")
    public PageResult2 listMyTicket(@RequestBody SearchEntity entity) {
        Page<TOrder> queryPage = new Page<>(entity.getPage(), entity.getSize());
        return PageUtil.getPageResult2(customerGoodsService.listMyTicket(queryPage, entity));
    }

    /**
     * @Description 热词列表
     * @Date 2024/9/3 22:13
     * @Author YWX
     * @Param [entity]
     * @Return cn.psvmc.cxzapi.response.Result
     **/
    @PostMapping("/list_hotword")
    public Result listHotword(@RequestBody SearchEntity entity) {
        String name = entity.getName();
        return ResultGenerator.genSuccessResult("查询成功！", customerGoodsService.listHotword(name));
    }
}
